﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
//using Microsoft.Exchange.WebServices.Data;
using OpenSmtp;
using OpenSmtp.Mail;

namespace Wicresoft.SMS.Common
{
    public class MailOperation
    {
       // private static ExchangeService service;

        /// <summary>
        /// 在调用静态方法时必须先构造访问邮件的对象
        /// </summary>
        /// <param name="esv">Exchange的版本,目前支持Exchange2007_SP1,Exchange2010,Exchange2010_SP1</param>
        /// <param name="ServerUrl">服务器的地址</param>
        /// <param name="Username">用户名</param>
        /// <param name="Password">密码</param>
        /// <param name="Domain">域</param>
        /// <param name="Email">邮件地址</param>
        public static void InstanceExchangeServerAPI(string evInfo, string ServerUrl, string Username, string Password, string Domain, string Email)
        {
            //if (service == null)
            //{
            //    switch (evInfo)
            //    {
            //        case "Exchange2007_SP1":
            //            service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            //            break;
            //        case "Exchange2010":
            //            service = new ExchangeService(ExchangeVersion.Exchange2010);
            //            break;
            //        case "Exchange2010_SP1":
            //            service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
            //            break;
            //        default:
            //            Log.WriteError("{0}", "传入的ExchangeServer版本信息有误");
            //            return;
            //    }
            //    //参数是用户名,密码,域
            //    service.Credentials = new WebCredentials(Username, Password, Domain);
            //    //给出Exchange Server的URL http://xxxxxxx 
            //    service.Url = new Uri(ServerUrl);
            //    //你自己的邮件地址 xxx@xxx.xxx
            //    service.AutodiscoverUrl(Email);
            //}
        }
        
        private static string UserName;
        private static string Password;
        private static string Host;
        private static int Port;
        private static string Sender;

        public static void InstanceSmtpClient(string username, string password, string host, string port,string sender)
        {
            UserName = username;
            Password = password;
            Sender = sender;
            Host = host;
            Port = Convert.ToInt32(port);          

        }

        /// <summary>
        /// 发送邮件, 仅供发送邮件的TASK使用
        /// </summary>
        /// <param name="email">邮件对象属于Wicresoft.SMS.Common</param>
        /// <returns></returns>
        public static bool SendEmail(EmailInfo email)
        {
            if ( Host == null)
            {
                Logger.Error("未配置邮件SMTP帐号信息");
                return false;
            }
            try
            {
                //OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(Host, UserName, Password,Port);
                Smtp smtp = new Smtp(Host, Port);
                MailMessage mm = BuildMailMessage(email);
                smtp.SendMail(mm);
                return true;
                //EmailMessage em = new EmailMessage(service);
                //em.Subject = email.Subject;
                //em.Body = email.Body;
                //AddEmailAddress2List(email.To, em.ToRecipients);
                //AddEmailAddress2List(email.CC, em.CcRecipients);
                //AddEmailAddress2List(email.BCC, em.BccRecipients);
                //em.SendAndSaveCopy();
                //return true;
            }
            catch (Exception ex)
            {
                Logger.Error("MailOperation.SendEmail " + ex.Message);
                return false;
            }
            
        }

        private static MailMessage BuildMailMessage(EmailInfo em)
        {
            MailMessage mm = new MailMessage();
            mm.Charset = "UTF-8";
            mm.From = new EmailAddress(Sender);
            AddEmailAddress2List(em.To, mm.To);
            AddEmailAddress2List(em.CC, mm.CC);
            AddEmailAddress2List(em.BCC, mm.BCC);
            mm.Subject = em.Subject;
            mm.HtmlBody = em.Body;
            return mm;
        }

        private static void AddEmailAddress2List(string Emails, System.Collections.ArrayList eac)
        {
            if (Emails == null)
            {
                return;
            }
            string[] email = Emails.Split(';');
            foreach (var item in email)
            {
                if (Regex.IsMatch(item, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                    eac.Add(new EmailAddress(item));
            }
        }

    }
}
